package org.weed.remote.service;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;

public class MouseService extends Thread {
	private int UNIT = 1;
	private int DELAY = 10;
	private Robot robot;
	private int vx;
	private int vy;
	private boolean isPaused;
	
	public MouseService() {
		try {
			robot = new Robot();
		} catch (AWTException e) {
		}
		isPaused = false;
		vx = 0;
		vy = 0;
	}
	
	public void setSpeed(int x, int y) {
		System.out.println("Speed x: "+x+", y: " + y);
		this.vx = x;
		this.vy = y;
	}
	
	@Override
	public void run() {
		System.out.println("Mouse service starts...");
		isPaused = false;
		while (!isPaused) {
			Point position = MouseInfo.getPointerInfo().getLocation();
			//System.out.println("x: "+vx+", y: "+vy);
			robot.mouseMove(position.x + vx , position.y + vy);
			try {
				Thread.sleep(40);
			} catch (InterruptedException e) {
			}
		}
	}
	
	public boolean isRunning() {
		return !isPaused;
	}
	
	public void mouseMove(int x, int y) {
		Point position = MouseInfo.getPointerInfo().getLocation();
		robot.mouseMove(position.x + x , position.y + y);
	}
	
	public void mouseClick(int button) {
		robot.mousePress(button);
		robot.mouseRelease(button);
	}
		
	public void pause() {
		System.out.println("Mouse service paused...");
		isPaused = true;
	}
}
